package com.kehutong.webchat.admin;

import com.kehutong.common.entity.Root;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.util.Token;
import com.kehutong.webchat.entity.MemberMap;
import com.kehutong.webchat.entity.WeChatSetting;
import com.kehutong.webchat.enums.WeChatType;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.execute.entity.FindOne;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户账号绑定的微信的记录
 * @author: liukunlong
 * @date: 2021-08-16 10:21
 */
@MvcAction
@ReqMapping("/admin/wechat/member/map")
public class MemberMapController {
    @Inject
    private JdbcSession jdbcSession;

    /**
     * 用户账号绑定的微信的记录列表
     * @param token
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/list")
    public Object list(Token token, JSONObject jsonObject) throws Exception {
        //首先查看公司有没有单独的小程序及公众号配置
        List<WeChatSetting> weChatSettingList = jdbcSession.findArray(WeChatSetting.class)
                .eq("companyNo", token.getCompanyName())
                .eq("type",WeChatType.xiaochengxu)
                .eq("deleted", false)
                .exe();
        //公司没有单独的小程序及公众号配置,则获取系统默认的小程序及公司配置
        if (Objects.isEmpty(weChatSettingList)) {
            weChatSettingList = jdbcSession.findArray(WeChatSetting.class)
                    .eq("companyNo", Root.ROOT_ID)
                    .eq("type",WeChatType.xiaochengxu)
                    .eq("deleted", false)
                    .exe();
        }
        List<String > mnAppIdList=new ArrayList<>();
        weChatSettingList.forEach(v->{
            mnAppIdList.add(v.getAppId());
        });
        List<MemberMap> list=jdbcSession.findArray(MemberMap.class)
                .like(jsonObject,"khtId").in("mn_app_id",mnAppIdList).exe();
        return Result.success(list);
    }
}
